package com.oceanstar.controller;

import com.oceanstar.Pojo.*;
import com.oceanstar.service.IMallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "*")
public class MallController {

    @Autowired
    private IMallService mallService;

    /*
     *该接口用于管理员新增服务商城中的服务项目（如家政保洁、上门维修、陪护照护等）
     */
    @PostMapping("/mall/service")
    public Result publicService(@RequestBody MallService service, @RequestHeader("token") String token) {
        try {
            mallService.publicService(service,token);
            return Result.success();
        } catch (Exception e){
            return Result.error("新增失败: " + e.getMessage());
        }
    }

    /*
     *该接口用于管理员修改服务项目信息（名称、价格、描述、上架状态等）。
     */
    @PatchMapping("/mall/service")
    public Result updateService(@RequestBody MallService service, @RequestHeader("token") String token) {
        try {
            mallService.updateService(service,token);
            return Result.success();
        } catch (Exception e){
            return Result.error("更新失败: " + e.getMessage());
        }
    }

    /*
     *该接口用于管理员批量删除服务项目。
     */
    @DeleteMapping("/mall/service")
    public Result deleteService(@RequestParam String ids, @RequestHeader("token") String token) {
        try {
            mallService.deleteServices(ids,token);
            return Result.success();
        } catch (Exception e){
            return Result.error("删除失败: " + e.getMessage());
        }
    }

    /*
     * 该接口用于查询服务商城中的服务项目列表
     */
    @GetMapping("/mall/services")
    public Result getServiceList(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) Integer minPrice,
            @RequestParam(required = false) Integer maxPrice,
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestHeader("token") String token) {
        try {
            PageInfo<MallService> result = mallService.getServiceList(
                    name, category, minPrice, maxPrice, status, page, pageSize, token);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /*
     * 该接口用于查询单个服务项目的详细信息
     */
    @GetMapping("/mall/service/detail")
    public Result getServiceDetail(@RequestParam Integer id, @RequestHeader("token") String token) {
        try {
            MallService service = mallService.getServiceDetail(id, token);
            return Result.success(service);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /*
     * 该接口用于老人或家属在服务商城中对某个服务项目进行下单
     */
    @PostMapping("/mall/order")
    public Result submitOrder(@RequestBody OrderSubmitDTO orderDTO, @RequestHeader("token") String token) {
        try {
            OrderSubmitResult result = mallService.submitOrder(orderDTO, token);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("下单失败: " + e.getMessage());
        }
    }

    /*
     * 该接口用于老人或家属查询自己的服务订单列表
     */
    @GetMapping("/mall/orders")
    public Result getMyOrders(
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestHeader("token") String token) {
        try {
            PageInfo<OrderVO> result = mallService.getMyOrders(status, page, pageSize, token);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /*
     * 该接口用于老人或家属取消尚未完成的服务订单
     */
    @PostMapping("/mall/order/cancel")
    public Result cancelOrder(@RequestBody OrderSubmitResult cancelRequest, @RequestHeader("token") String token) {
        try {
            mallService.cancelOrder(cancelRequest.getOrderId(), token);
            return Result.success();
        } catch (Exception e) {
            return Result.error("取消订单失败: " + e.getMessage());
        }
    }

    /*
     * 该接口用于管理员查询服务商城中所有服务订单列表
     */
    @GetMapping("/mall/admin/orders")
    public Result getAdminOrders(
            @RequestParam(required = false) String orderId,
            @RequestParam(required = false) String serviceName,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String beginDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(required = false) String elderName,
            @RequestParam(required = false) String contactPhone,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestHeader("token") String token) {
        try {
            PageInfo<MallOrder> result = mallService.getAdminOrders(
                    orderId, serviceName, status, beginDate, endDate, elderName, contactPhone, page, pageSize, token);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /*
     * 该接口用于管理员或后台运营人员对服务订单进行处理
     */
    @PostMapping("/mall/admin/order/handle")
    public Result handleOrder(@RequestBody OrderHandleDTO handleDTO, @RequestHeader("token") String token) {
        try {
            mallService.handleOrder(handleDTO, token);
            return Result.success();
        } catch (Exception e) {
            return Result.error("处理订单失败: " + e.getMessage());
        }
    }
}
